home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2008 December / PC_Format_122008.iso / Multimedia / MediaPortal 0.2.3.0 / MediaPortal_0.2.3.0_Setup.exe / scripts / imdb / yahoo_it.csscript < prev   
Text File  |  2007-10-12  |  17KB  |  523 lines

  1. //css_reference "core.dll";
  2. //css_reference "Databases.dll";
  3. //css_reference "utils.dll";
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using System.IO;
  13. using System.Net;
  14. using System.Collections;
  15. using System.Web;
  16. using System.Text.RegularExpressions;
  17. using MediaPortal.Util;
  18.  
  19. class Grabber : MediaPortal.Video.Database.IIMDBScriptGrabber
  20. {
  21.   public Grabber()
  22.     {
  23.     }
  24.   void MediaPortal.Video.Database.IIMDBScriptGrabber.FindFilm(string strSearch, int iLimit, ArrayList elements)
  25.   {
  26.     int iCount = 0;
  27.     string strTitle;
  28.     try
  29.     {
  30.       string absoluteUri;
  31.       string strURL = "http://it.movies.yahoo.com/dynamic/textsearch.html?p=" + strSearch + "&sp=movie";
  32.       string strBody = GetPage(strURL, "utf-8", out absoluteUri);
  33.  
  34.       // Mars Warrior @ 03-sep-2004.
  35.       // First try to find an Exact Match. If no exact match found, just look
  36.       // for any match and add all those to the list. This narrows it down more easily...
  37.       int iStartOfMovieList = strBody.IndexOf("<h2> Film </h2>");
  38.       int endOfTitleList = strBody.IndexOf("Notizie </h2>");
  39.       if (iStartOfMovieList < 0)
  40.       {
  41.         return;
  42.       }
  43.  
  44.       iStartOfMovieList += "<ol".Length;
  45.       int iEndOfMovieList = strBody.IndexOf("</ol>", iStartOfMovieList);
  46.  
  47.       if (iEndOfMovieList < 0)
  48.       {
  49.         iEndOfMovieList = strBody.Length;
  50.       }
  51.       if (endOfTitleList < iEndOfMovieList && endOfTitleList > iStartOfMovieList)
  52.       {
  53.         iEndOfMovieList = endOfTitleList;
  54.       }
  55.       strBody = strBody.Substring(iStartOfMovieList, iEndOfMovieList - iStartOfMovieList);
  56.       while ((true) && (iCount < iLimit))
  57.       {
  58.         ////<A HREF="/Title?0167261">Lord of the Rings: The Two Towers, The (2002)</A>
  59.         int iAHREF = strBody.IndexOf("<a href=");
  60.         if (iAHREF >= 0)
  61.         {
  62.           int iEndAHREF = strBody.IndexOf("</a>");
  63.           if (iEndAHREF >= 0)
  64.           {
  65.             iAHREF += "<a href=.".Length;
  66.             string strAHRef = strBody.Substring(iAHREF, iEndAHREF - iAHREF);
  67.             int iURL = strAHRef.IndexOf(">");
  68.             if (iURL > 0)
  69.             {
  70.               strTitle = "";
  71.               strURL = strAHRef.Substring(0, iURL);
  72.               //if (strURL[strURL.Length - 1] == '\'')
  73.                 strURL = strURL.Substring(0, strURL.Length - 1);
  74.               iURL++;
  75.               int iURLEnd = strAHRef.IndexOf("<", iURL);
  76.               if (iURLEnd > 0)
  77.               {
  78.                 strTitle = strAHRef.Substring(iURL, iURLEnd - iURL);
  79.               }
  80.               else
  81.               strTitle = strAHRef.Substring(iURL);
  82.  
  83.               ///int onclick = strURL.IndexOf(" onclick");
  84.               ///if (onclick >= 0)
  85.               ///  strURL = strURL.Substring(0, onclick - 1);
  86.               strURL = String.Format("http://it.movies.yahoo.com{0}", strURL);
  87.               HTMLUtil htmlUtil = new HTMLUtil();
  88.               htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
  89.  
  90.  
  91.               int endTagLength = "</a>".Length;
  92.               int posNextTag = strBody.IndexOf("<", iEndAHREF + endTagLength);
  93.               if (posNextTag > 0)
  94.               {
  95.               string strSub = strBody.Substring(iEndAHREF + endTagLength, posNextTag - (iEndAHREF + endTagLength));
  96.               strTitle += strSub;
  97.               }
  98.               // to avoid including of   
  99.               if ((strTitle.IndexOf("\n") < 0) && (strTitle.IndexOf(" ") < 0))
  100.               {
  101.                 MediaPortal.Video.Database.IMDB.IMDBUrl url = new MediaPortal.Video.Database.IMDB.IMDBUrl(strURL, strTitle + " (yahoo_it)", "yahoo_it");
  102.                 elements.Add(url);
  103.               }
  104.               iCount++;
  105.             }
  106.             if (iEndAHREF + 1 >= strBody.Length)
  107.               break;
  108.             iStartOfMovieList = iEndAHREF + 1;
  109.             strBody = strBody.Substring(iEndAHREF + 1);
  110.           }
  111.           else
  112.           {
  113.             break;
  114.           }
  115.         }
  116.         else
  117.         {
  118.           break;
  119.         }
  120.       }
  121.     }
  122.     catch (Exception ex)
  123.     {
  124.       MediaPortal.GUI.Library.Log.Error("exception for yahoo lookup of {0} err:{1} stack:{2}",strSearch, ex.Message, ex.StackTrace);
  125.     }
  126.   }
  127.  
  128.   bool MediaPortal.Video.Database.IIMDBScriptGrabber.GetDetails(MediaPortal.Video.Database.IMDB.IMDBUrl url, ref MediaPortal.Video.Database.IMDBMovie movieDetails)
  129.   {
  130.     try
  131.     {
  132.  
  133.       int iStart = 0;
  134.       int iEnd = 0;
  135.       movieDetails.Reset();
  136.       // add databaseinfo
  137.       // may add an another grabber  
  138.       movieDetails.Database = "yahoo_it";
  139.  
  140.       string strAbsURL;
  141.       string strAbsMovieURL;
  142.       string strUrlMovieURL;
  143.       string strBody = GetPage(url.URL, "utf-8", out strAbsURL);
  144.       strAbsMovieURL = strAbsURL;
  145.       if (strBody == null || strBody.Length == 0)
  146.         return false;
  147.  
  148.       int iPos = strAbsURL.IndexOf("index-");
  149.       if (iPos > 0)
  150.       {
  151.         iPos += "index-".Length;
  152.         movieDetails.IMDBNumber = strAbsURL.Substring(iPos);
  153.         int pos = movieDetails.IMDBNumber.IndexOf(".html");
  154.         if (pos > 0)
  155.           movieDetails.IMDBNumber = movieDetails.IMDBNumber.Substring(0, pos);
  156.       }
  157.  
  158.       int imPos = strAbsMovieURL.IndexOf("yahoo.com");
  159.       if (imPos > 0)
  160.       {
  161.         imPos += "yahoo.com".Length;
  162.         strUrlMovieURL = strAbsMovieURL.Substring(imPos);
  163.         int posm = strUrlMovieURL.IndexOf("index-");
  164.         if (posm > 0)
  165.             strUrlMovieURL = strUrlMovieURL.Substring(0, posm);
  166.       }
  167.       
  168.       url.Title = url.Title.Trim();
  169.       // cut of " (imdb)"
  170.       iEnd = url.Title.IndexOf("(");
  171.       if (iEnd >= 0)
  172.         movieDetails.Title = url.Title.Substring(0, iEnd);
  173.       else
  174.         movieDetails.Title = url.Title;
  175.       movieDetails.Title = movieDetails.Title.Trim();
  176.       string movieTitle = System.Web.HttpUtility.HtmlEncode(movieDetails.Title);
  177.       int iDirectedBy = strBody.IndexOf("Regia");
  178.       int iCredits = strBody.IndexOf("Sceneggiatura");
  179.       int iGenre = strBody.IndexOf("Genere");
  180.       if (iGenre < 0)
  181.          iGenre = strBody.IndexOf("Generi");
  182.       ///int iTagLine = strBody.IndexOf("Tagline:</h5>");
  183.       int iPlotOutline = strBody.IndexOf("Trama");
  184.       int iPlotSummary = strBody.IndexOf("Trama");
  185.       int iPlot = strBody.IndexOf("Trama");
  186.       int iImage = strBody.IndexOf( movieDetails.IMDBNumber + ".html\'><img src=\"");
  187.       if (iImage >= 0)
  188.       {
  189.         iImage += (movieDetails.IMDBNumber + ".html\'><img src=\"").Length;
  190.       }
  191.       int iRating = strBody.IndexOf("numrating");
  192.       int iCred = strBody.IndexOf("<dt>Cast</dt>");
  193.       ///int iTop = strBody.IndexOf("Top 250:");
  194.       int iTop = -1;
  195.       int iYear = strBody.IndexOf("Data di uscita");
  196.       if (iYear >= 0)
  197.       {
  198.         int iYearEnd = strBody.IndexOf("</dd>", iYear);
  199.         iYearEnd -= 4;
  200.         string strYear = strBody.Substring(iYearEnd, 4);
  201.         movieDetails.Year = System.Int32.Parse(strYear);
  202.       }
  203.  
  204.       if (iDirectedBy >= 0)
  205.         movieDetails.Director = ParseAHREFIMDB(strBody, iDirectedBy, url.URL).Trim();
  206.  
  207.       if (iCredits >= 0)
  208.         movieDetails.WritingCredits = ParseAHREFIMDB(strBody, iCredits, url.URL).Trim();
  209.  
  210.       if (iGenre >= 0)
  211.       {
  212.         iGenre += "Genere</dt><dd>".Length;
  213.         int iGenreEnd = strBody.IndexOf("</dd>", iGenre);
  214.         movieDetails.Genre = strBody.Substring(iGenre, iGenreEnd - iGenre);
  215.       }
  216.       if (iRating >= 0) // and votes
  217.       {
  218.         iRating += "numrating".Length;
  219.         iStart = strBody.IndexOf("alt=\"", iRating);
  220.         if (iStart >= 0)
  221.         {
  222.           iStart += "alt=\"".Length;
  223.           iEnd = strBody.IndexOf("stars", iStart);
  224.           iEnd -= 1;
  225.           // set rating
  226.           string strRating = strBody.Substring(iStart, iEnd - iStart);
  227.           if (strRating != String.Empty)
  228.             strRating = strRating.Replace('.', ',');
  229.           try
  230.           {
  231.             movieDetails.Rating = (float)System.Double.Parse(strRating);
  232.             movieDetails.Rating = movieDetails.Rating * 2;
  233.             if (movieDetails.Rating > 10.0f)
  234.               movieDetails.Rating /= 10.0f;
  235.           }
  236.           catch (Exception)
  237.           {
  238.           }
  239.           movieDetails.Votes = "0";
  240.           ///if (movieDetails.Rating != 0.0f)
  241.           ///{
  242.             // now, votes
  243.           ///  movieDetails.Votes = "0";
  244.           ///  iStart = strBody.IndexOf("(", iEnd + 2);
  245.           ///  if (iStart > 0)
  246.           ///  {
  247.           ///    iEnd = strBody.IndexOf(" votes</a>)", iStart);
  248.           ///    if (iEnd > 0)
  249.           ///    {
  250.           ///      iStart += "(<a href=\"ratings\">".Length; // skip the parantese and link before votes
  251.           ///      movieDetails.Votes = strBody.Substring(iStart, iEnd - iStart).Trim();
  252.           ///    }
  253.           ///  }
  254.           ///}
  255.         }
  256.       }
  257.  
  258.       ///if (iTop >= 0) // top rated movie :)
  259.       ///{
  260.       ///  iTop += "top 250:".Length + 2; // jump space and #
  261.       ///  iEnd = strBody.IndexOf("</a>", iTop);
  262.       ///  string strTop = strBody.Substring(iTop, iEnd - iTop);
  263.       ///  movieDetails.Top250 = System.Int32.Parse(strTop);
  264.       ///}
  265.       ///if (iTagLine >= 0)
  266.       ///{
  267.       ///  iTagLine += "Tagline:</h5>".Length;
  268.       ///  iEnd = strBody.IndexOf("<", iTagLine);
  269.       ///  movieDetails.TagLine = strBody.Substring(iTagLine, iEnd - iTagLine).Trim();
  270.       ///  movieDetails.TagLine = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.TagLine);
  271.       ///  movieDetails.TagLine = HttpUtility.HtmlDecode(movieDetails.TagLine);  // Remove HTML entities like ½
  272.       ///}
  273.  
  274.       if (iPlotOutline < 0)
  275.       {
  276.         if (iPlotSummary > 0)
  277.         {
  278.           iPlotSummary += "Plot Summary:</h5>".Length;
  279.           iEnd = strBody.IndexOf("<", iPlotSummary);
  280.           movieDetails.PlotOutline = strBody.Substring(iPlotSummary, iEnd - iPlotSummary).Trim();
  281.           movieDetails.PlotOutline = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.PlotOutline);
  282.           movieDetails.PlotOutline = HttpUtility.HtmlDecode(movieDetails.PlotOutline);  // remove HTML entities
  283.         }
  284.       }
  285.       else
  286.       {
  287.         iPlotOutline += "Trama</h3>".Length;
  288.         iPlotOutline = strBody.IndexOf("<p>", iPlotOutline);
  289.         iPlotOutline += "<p>".Length;
  290.         iEnd = strBody.IndexOf("<p>", iPlotOutline);
  291.         movieDetails.PlotOutline = strBody.Substring(iPlotOutline, iEnd - iPlotOutline).Trim();
  292.         movieDetails.PlotOutline = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.PlotOutline);
  293.         movieDetails.PlotOutline = HttpUtility.HtmlDecode(movieDetails.PlotOutline);  // remove HTML entities
  294.         movieDetails.Plot = movieDetails.PlotOutline.Trim();
  295.         movieDetails.Plot = HttpUtility.HtmlDecode(movieDetails.Plot);  // remove HTML entities
  296.       }
  297.  
  298.       if (iImage >= 0)
  299.       {
  300.         iEnd = strBody.IndexOf("\"", iImage);
  301.         movieDetails.ThumbURL = strBody.Substring(iImage, iEnd - iImage).Trim();
  302.       }
  303.  
  304.       //plot (removed)
  305.       
  306.  
  307.       //cast
  308.       int iCredEnd = strBody.IndexOf("</dd>", iCred);
  309.       int iEndActor = -1;
  310.       iCred += "<dd>".Length;
  311.       if (iCred >= 0)
  312.       {
  313.         string strCast = strBody.Substring(iCred, iCredEnd - iCred);
  314.         //movieDetails.Cast += strCast;
  315.         while ((true))
  316.         {
  317.           int iActor = strCast.IndexOf("\'>");
  318.           if (iActor >= 0)
  319.           {
  320.             iEndActor = strCast.IndexOf("</a>");
  321.             if (iEndActor >= 0)
  322.             {
  323.               iActor += "\'>".Length;
  324.               string strActor = strCast.Substring(iActor, iEndActor - iActor);
  325.               strActor = MediaPortal.Util.Utils.stripHTMLtags(strActor).Trim();
  326.               strActor = HttpUtility.HtmlDecode(strActor);
  327.               movieDetails.Cast += strActor;
  328.               movieDetails.Cast += "\n";
  329.             }
  330.           }
  331.           else
  332.           {
  333.             break;
  334.           }
  335.           if (iEndActor + 1 >= strCast.Length)
  336.             break;
  337.           iCred = iEndActor + 1;
  338.           strCast = strCast.Substring(iEndActor + 1);
  339.         }
  340.       }
  341.  
  342.       int iRunTime = strBody.IndexOf("Durata");
  343.       if (iRunTime > 0)
  344.       {
  345.         iRunTime += "Durata</dt><dd>".Length;
  346.         string runtime = "";
  347.         runtime = strBody.Substring(iRunTime,2);
  348.         try
  349.         {
  350.            movieDetails.RunTime = Int32.Parse(runtime);
  351.         }
  352.         catch (Exception) { }
  353.         movieDetails.RunTime = movieDetails.RunTime * 60;
  354.         iRunTime++;
  355.         iRunTime++;
  356.         iRunTime++;
  357.         runtime = strBody.Substring(iRunTime, 2);
  358.         try
  359.         {
  360.            movieDetails.RunTime = movieDetails.RunTime + Int32.Parse(runtime);
  361.         }
  362.         catch (Exception) { }
  363.         
  364.       }
  365.  
  366.       int mpaa = strBody.IndexOf("MPAA</a>:</h5>");
  367.       if (mpaa > 0)
  368.       {
  369.         mpaa += "MPAA</a>:</h5>".Length;
  370.         int mpaaEnd = strBody.IndexOf("</div>", mpaa);
  371.         if (mpaaEnd > 0)
  372.         {
  373.           movieDetails.MPARating = strBody.Substring(mpaa, mpaaEnd - mpaa);
  374.         }
  375.       }
  376.  
  377.  
  378.       return true;
  379.     }
  380.     catch (Exception ex)
  381.     {
  382.       MediaPortal.GUI.Library.Log.Error("exception for imdb lookup of {0} err:{1} stack:{2}", url.URL, ex.Message, ex.StackTrace);
  383.     }
  384.     return false;
  385.   }
  386.  
  387.   string MediaPortal.Video.Database.IIMDBScriptGrabber.GetName()
  388.   {
  389.     return "YAHOOIT grabber ";
  390.   }
  391.  
  392.   string MediaPortal.Video.Database.IIMDBScriptGrabber.GetLanguage()
  393.   {
  394.     return "IT";
  395.   }
  396.  
  397.   private string GetPage(string strURL, string strEncode, out string absoluteUri)
  398.   {
  399.     string strBody = "";
  400.     absoluteUri = String.Empty;
  401.     Stream ReceiveStream = null;
  402.     StreamReader sr = null;
  403.     WebResponse result = null;
  404.     try
  405.     {
  406.       // Make the Webrequest
  407.       //Log.Info("IMDB: get page:{0}", strURL);
  408.       WebRequest req = WebRequest.Create(strURL);
  409.  
  410.       result = req.GetResponse();
  411.       ReceiveStream = result.GetResponseStream();
  412.  
  413.       // Encoding: depends on selected page
  414.       Encoding encode = System.Text.Encoding.GetEncoding(strEncode);
  415.       sr = new StreamReader(ReceiveStream, encode);
  416.       strBody = sr.ReadToEnd();
  417.  
  418.       absoluteUri = result.ResponseUri.AbsoluteUri;
  419.     }
  420.     catch (Exception)
  421.     {
  422.       //Log.Error("Error retreiving WebPage: {0} Encoding:{1} err:{2} stack:{3}", strURL, strEncode, ex.Message, ex.StackTrace);
  423.     }
  424.     finally
  425.     {
  426.       if (sr != null)
  427.       {
  428.         try
  429.         {
  430.           sr.Close();
  431.         }
  432.         catch (Exception)
  433.         {
  434.         }
  435.       }
  436.       if (ReceiveStream != null)
  437.       {
  438.         try
  439.         {
  440.           ReceiveStream.Close();
  441.         }
  442.         catch (Exception)
  443.         {
  444.         }
  445.       }
  446.       if (result != null)
  447.       {
  448.         try
  449.         {
  450.           result.Close();
  451.         }
  452.         catch (Exception)
  453.         {
  454.         }
  455.       }
  456.     }
  457.     return strBody;
  458.   } // END GetPage()
  459.   string ParseAHREFIMDB(string strBody, int iahref, string strURL)
  460.   {
  461.     int iStart = strBody.IndexOf("<a href=\'", iahref);
  462.     if (iStart < 0)
  463.       iStart = strBody.IndexOf("<A HREF=\'", iahref);
  464.     if (iStart < 0)
  465.       return "";
  466.  
  467.     int iEnd = strBody.IndexOf("</a>", iStart);
  468.     if (iEnd < 0)
  469.       iEnd = strBody.IndexOf("</A>", iStart);
  470.     if (iEnd < 0)
  471.       return "";
  472.  
  473.     iStart += "<a href=\'".Length;
  474.     int iSep = strBody.IndexOf(">", iStart);
  475.     string strurl = strBody.Substring(iStart, (iSep - iStart) - 1);
  476.     iSep++;
  477.     string strTitle = strBody.Substring(iSep, iEnd - iSep);
  478.     strTitle = MediaPortal.Util.Utils.stripHTMLtags(strTitle);
  479.     HTMLUtil htmlUtil = new HTMLUtil();
  480.     htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
  481.     strTitle = strTitle.Trim();
  482.     return strTitle.Trim();
  483.  
  484.   }
  485.   string ParseGenresIMDB(string strBody, int iGenre, string url)
  486.   {
  487.     string strTmp;
  488.     string strTitle = "";
  489.     string strHRef = strBody.Substring(iGenre);
  490.     int iSlash = strHRef.IndexOf(" / ");
  491.     int iEnd = 0;
  492.     int iStart = 0;
  493.     if (iSlash >= 0)
  494.     {
  495.       int iRealEnd = strHRef.IndexOf(">more<");
  496.       if (iRealEnd < 0)
  497.         iRealEnd = strHRef.IndexOf("</div>");
  498.       while (iSlash < iRealEnd)
  499.       {
  500.         iStart = iEnd + 2;
  501.         iEnd = iSlash;
  502.         int iLen = iEnd - iStart;
  503.         if (iLen < 0)
  504.           break;
  505.         strTmp = strHRef.Substring(iStart, iLen);
  506.         strTitle = strTitle + ParseAHREFIMDB(strTmp, 0, "") + " / ";
  507.  
  508.         iSlash = strHRef.IndexOf(" / ", iEnd + 2);
  509.         if (iSlash < 0)
  510.           iSlash = iRealEnd;
  511.       }
  512.     }
  513.     // last genre
  514.     iEnd += 2;
  515.     strTmp = strHRef.Substring(iEnd);
  516.     strTitle = strTitle + ParseAHREFIMDB(strTmp, 0, "");
  517.     HTMLUtil htmlUtil = new HTMLUtil();
  518.     htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
  519.  
  520.     return strTitle;
  521.   }
  522.  
  523. }